package com.see.front.data;

import java.util.ArrayList;
import java.util.List;

import com.see.domain.dao.DataNucleusUtil;
import com.see.domain.model.StaticData;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

public final class StaticDataAdaptor {

    public static final Object      TITLE_SYMBOL      = "symbol";
    public static final Object      TITLE_SECTOR      = "sector";
    public static final Object      TITLE_INDUSTRY    = "industry";
    private static DataNucleusUtil  util              = new DataNucleusUtil();
    private static List<StaticData> listOfRecords     = new ArrayList();
    private static IndexedContainer container         = container = new IndexedContainer();

    public static IndexedContainer getSymbolContainer(String symbol) {
        fillStaticDataContainer(container, TITLE_SYMBOL.toString(), symbol);
        return container;
    }

    public static IndexedContainer getSectorContainer(String sector) {
        fillStaticDataContainer(container, TITLE_SECTOR.toString(), sector);
        return container;
    }

    public static IndexedContainer getIndustryContainer(String industry) {
        fillStaticDataContainer(container, TITLE_INDUSTRY.toString(), industry);
        return container;
    }

    private static void fillStaticDataContainer(IndexedContainer container, String indicator, String keyWord) {
        container.removeAllItems();
        container.addContainerProperty(TITLE_SYMBOL, String.class, null);
        container.addContainerProperty(TITLE_SECTOR, String.class, null);
        container.addContainerProperty(TITLE_INDUSTRY, String.class, null);

        // get full list by default
        if (indicator.equals(TITLE_INDUSTRY.toString())) {
            listOfRecords = util.getRecordByIndustry(keyWord);
        } else if (indicator.equals(TITLE_SECTOR.toString())) {
            listOfRecords = util.getRecordBySector(keyWord);
        } else if (indicator.equals(TITLE_SYMBOL.toString())){
            listOfRecords = util.getFullListOfRecords();
        }

        if (listOfRecords.size() > 0) {
            for (StaticData staticData : listOfRecords) {
                Item item = container.addItem(staticData.getSymbol());
                item.getItemProperty(TITLE_SYMBOL).setValue(staticData.getSymbol());
                item.getItemProperty(TITLE_SECTOR).setValue(staticData.getSector());
                item.getItemProperty(TITLE_INDUSTRY).setValue(staticData.getIndustry());
            }
        }
        container.sort(new Object[] {TITLE_SYMBOL}, new boolean[] {true});
       

    }

}
